﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model;
using l1cs.server.datatables;
using l1cs.server.model.Instance;

namespace l1cs.server.serverpackets
{
    class S_NPCPack : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_NPCPack ).FullName );

        private const int STATUS_POISON = 1;
        private const int STATUS_INVISIBLE = 2;
        private const int STATUS_PC = 4;
        private const int STATUS_FREEZE = 8;
        private const int STATUS_BRAVE = 16;
        private const int STATUS_ELFBRAVE = 32;
        private const int STATUS_FASTMOVABLE = 64;
        private const int STATUS_GHOST = 128;

        private byte[] _byte = null;

        public S_NPCPack( L1NpcInstance npc )
        {
            writeC( Opcodes.S_OPCODE_CHARPACK );
            writeH( npc.getX() );
            writeH( npc.getY() );
            writeD( npc.getId() );
            if ( npc.getTempCharGfx() == 0 )
            {
                writeH( npc.getGfxId() );
            }
            else
            {
                writeH( npc.getTempCharGfx() );
            }
            if ( npc.getNpcTemplate().is_doppel() && npc.getGfxId() != 31 )
            { // スライムの姿をしていなければドッペル
                writeC( 4 ); // 長剣
            }
            else
            {
                writeC( npc.getStatus() );
            }
            writeC( npc.getHeading() );
            writeC( npc.getChaLightSize() );
            writeC( npc.getMoveSpeed() );
            writeD( npc.getExp() );
            writeH( npc.getTempLawful() );
            writeS( npc.getNameId() );
            if ( npc is L1FieldObjectInstance )
            { // SICの壁字、看板など
                L1NpcTalkData talkdata = NPCTalkDataTable.getInstance().getTemplate( npc.getNpcTemplate().get_npcId() );
                if ( talkdata != null )
                {
                    writeS( talkdata.getNormalAction() ); // タイトルがHTML名として解釈される
                }
                else
                {
                    writeS( null );
                }
            }
            else
            {
                writeS( npc.getTitle() );
            }

            /**
             * ｼｼﾆﾃ - 0:mob,item(atk pointer), 1:poisoned(), 2:invisable(), 4:pc,
             * 8:cursed(), 16:brave(), 32:??, 64:??(??), 128:invisable but name
             */
            int status = 0;
            if ( npc.getPoison() != null )
            { // 毒状態
                if ( npc.getPoison().getEffectId() == 1 )
                {
                    status |= STATUS_POISON;
                }
            }
            if ( npc.getNpcTemplate().is_doppel() )
            {
                // PC属性だとエヴァの祝福を渡せないためWIZクエストのドッペルは例外
                if ( npc.getNpcTemplate().get_npcId() != 81069 )
                {
                    status |= STATUS_PC;
                }
            }
            writeC( status );

            writeD( 0 ); // 0以外にするとC_27が飛ぶ
            writeS( null );
            writeS( null ); // マスター名？
            writeC( 0 );
            writeC( 0xFF ); // HP
            writeC( 0 );
            writeC( npc.getLevel() );
            writeC( 0 );
            writeC( 0xFF );
            writeC( 0xFF );
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = _bao.ToArray();
            }

            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_NPCPack";
        }
    }
}
